package com.ayu.gulimall.coupon.service.impl;

import com.ayu.gulimall.coupon.dao.SeckillSkuRelationDao;
import com.ayu.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.ayu.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ayu.common.utils.PageUtils;
import com.ayu.common.utils.Query;

import com.ayu.gulimall.coupon.dao.SeckillSessionDao;
import com.ayu.gulimall.coupon.entity.SeckillSessionEntity;
import com.ayu.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSessionDao seckillSessionDao;
    @Autowired
    SeckillSkuRelationDao skuRelationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> listLast3() {
        String startTime = this.startTime();
        String endTime = this.endTime();
        List<SeckillSessionEntity> entityList = seckillSessionDao.selectList(new QueryWrapper<SeckillSessionEntity>()
                .between("start_time", startTime, endTime));

        if(entityList!=null){
            List<SeckillSessionEntity> collect = entityList.stream().map(session -> {
                List<SeckillSkuRelationEntity> selectList = skuRelationDao.selectList(new QueryWrapper<SeckillSkuRelationEntity>()
                        .eq("promotion_session_id", session.getId()));
                session.setSkuRelations(selectList);

                return session;
            }).collect(Collectors.toList());

            return collect;
        }

        return null;
    }


    //得到最近三天的时间
    public String startTime(){
        LocalDate localDate =LocalDate.now();
        LocalTime time =LocalTime.MIN;
        LocalDateTime startTime = LocalDateTime.of(localDate, time);
        String format = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    public String endTime(){
        LocalDate localDate =LocalDate.now();
        LocalDate end = localDate.plusDays(2);
        LocalTime time =LocalTime.MAX;
        LocalDateTime startTime = LocalDateTime.of(end, time);
        String format = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

}